<?PHP
require_once("../includes/config.inc.php");
validate_admin();
function cleanData(&$str)
{
	$str = preg_replace("/\t/", "\\t", $str);
	$str = preg_replace("/\r?\n/", "\\n", $str);
	if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// filename for download
$filename = "address_book_" . date('Ymd') . ".xls";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

$flag = false;
$sql = "SELECT 
		firstName as `First Name`,
		middleName as `Middle Name`,
		lastName as `Last Name`,
		email as `Email`
		FROM 
		tbl_address_book 
		ORDER BY firstName asc
		";
$result = mysql_query($sql) or die('Query failed!');
while(false !== ($row = mysql_fetch_assoc($result))) {
  if(!$flag) {
	  // display field/column names as first row
	  echo implode("\t", array_keys($row)) . "\r\n";
	  $flag = true;
	}
	array_walk($row, 'cleanData');
	echo implode("\t", array_values($row)) . "\r\n";
}
exit;
?>